<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="商品分类" prop="parentName">
        <el-popover
          ref="dataListPopover"
          placement="bottom-start"
          trigger="click">
          <el-tree
            :data="dataList"
            :props="dataListTreeProps"
            node-key="id"
            ref="dataListTree"
            @current-change="dataListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false">
          </el-tree>
        </el-popover>
        <el-input v-model="dataForm.parentName" v-popover:dataListPopover :readonly="true" placeholder="点击选择上级菜单" class="menu-list__input"></el-input>
      </el-form-item>
      <el-form-item label="商品名称" prop="productCode">
        <el-select v-model="dataForm.productCode" placeholder="商品名称" style="width:250px;height:50px">
          <el-option label="选择商品" value=""></el-option>
          <el-option v-for="(item, index) in dataForm.product" :label="item.productName" :key="item.id" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="入库数量" prop="inNum">
        <el-input v-model="dataForm.inNum" placeholder="入库数量"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { treeDataTranslate } from '@/utils'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          productCode: '',
          inNum: 0,
          status: '',
          remark: '',
          creator: '',
          createTime: '',
          updator: '',
          updateTime: '',
          pid: '',
          pid2: '',
          fatherCategory: [],
          category: [],
          product: [],
          parentId: 0,
          parentName: ''
        },
        dataRule: {
          productCode: [{
            required: true,
            message: '型号不能为空',
            trigger: 'blur'
          }],
          inNum: [{
            required: true,
            message: '入库数量不能为空',
            trigger: 'blur'
          }]
        },
        dataList: [],
        dataListTreeProps: {
          label: 'category',
          children: 'children'
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/business/ckin/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({
              data
            }) => {
              if (data && data.code === 0) {
                this.dataForm.productCode = data.ckIn.productCode
                this.dataForm.inNum = data.ckIn.inNum
                this.dataForm.status = data.ckIn.status
                this.dataForm.remark = data.ckIn.remark
                this.dataForm.creator = data.ckIn.creator
                this.dataForm.createTime = data.ckIn.createTime
                this.dataForm.updator = data.ckIn.updator
                this.dataForm.updateTime = data.ckIn.updateTime
              }
            })
          }
        })
      },
      // 查询一级分类
      getFatherCategory () {
        this.$http({
          url: this.$http.adornUrl('/business/ckcategory/list'),
          method: 'get',
          params: this.$http.adornParams({

          })
        }).then(({
          data
        }) => {
          this.dataList = treeDataTranslate(data, 'id', 'pid')
        })
      },
      // 查询产品
      getProduct (categoryId) {
        let cid = 0
        if (categoryId !== 0) {
          cid = categoryId
        } else {
          cid = ''
        }
        this.$http({
          url: this.$http.adornUrl('/business/ckproduct/listBy'),
          method: 'get',
          params: this.$http.adornParams({
            'categoryId': cid
          })
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.dataForm.product = data.productList
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        if (this.dataForm.inNum <= 0) {
          this.$message.error('数量不能为0或负')
          this.dataForm.inNum = 0
          return false
        }
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/business/ckin/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'productCode': this.dataForm.productCode,
                'inNum': this.dataForm.inNum,
                'status': this.dataForm.status,
                'remark': this.dataForm.remark,
                'creator': this.dataForm.creator,
                'createTime': this.dataForm.createTime,
                'updator': this.dataForm.updator,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({
              data
            }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // 菜单树选中
      dataListTreeCurrentChangeHandle (data, node) {
        this.dataForm.parentId = data.id
        this.dataForm.parentName = data.category
        this.pClose()
        this.getProduct(data.id)
      },
      pClose () {
        this.$refs[`dataListPopover`].doClose()
      }
    }
  }
</script>
